package com.samphanie.auiu.common.ui.component

import android.os.Bundle
import android.view.View
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.samphanie.auiu.common.R
import com.samphanie.auiu.common.databinding.FragmentListBinding
import com.samphanie.auiu.ui.item.AuiuAdapter
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.refresh.AuiuOverView
import com.samphanie.auiu.ui.refresh.AuiuRefresh
import com.samphanie.auiu.ui.refresh.AuiuTextOverView

/**
 * @author ZSY
 */
open class AuiuAbsListFragment : AuiuBaseFragment<FragmentListBinding>(),
    AuiuRefresh.AuiuRefreshListener {

    private lateinit var auiuAdapter: AuiuAdapter
    private lateinit var layoutManager: RecyclerView.LayoutManager
    private lateinit var refreshHeaderView: AuiuTextOverView

    var pageNum: Int = 1

    override fun getLayoutId(): Int {
        return R.layout.fragment_list
    }

    @CallSuper
    override fun initialize(savedInstanceState: Bundle?) {
        refreshHeaderView = AuiuTextOverView(requireContext())

        binding.refreshLayout.setRefreshOverView(refreshHeaderView)
        binding.refreshLayout.setRefreshListener(this)

        layoutManager = createLayoutManager()
        auiuAdapter = AuiuAdapter(requireContext())

        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = auiuAdapter

        binding.emptyView.visibility = View.GONE
        binding.emptyView.setIcon(R.string.list_empty)
        binding.emptyView.setDesc(getString(R.string.list_empty_desc))
        binding.emptyView.setButton(getString(R.string.list_empty_action), View.OnClickListener {
            onRefresh()
        })

        binding.contentLoading.visibility = View.VISIBLE
        pageNum = 1
    }

    fun finishRefresh(dataItems: List<AuiuDataItem<*, out RecyclerView.ViewHolder>>?) {
        val success = dataItems != null && dataItems.isNotEmpty()

        val refresh = pageNum == 1
        if (refresh) {
            binding.contentLoading.visibility = View.GONE
            binding.refreshLayout.refreshFinished()
            if (success) {
                binding.emptyView.visibility = View.GONE
                auiuAdapter.clearItems()
                auiuAdapter.addItems(dataItems!!, true)
            } else {
                if (auiuAdapter.itemCount <= 0) {
                    binding.emptyView.visibility = View.VISIBLE
                }
            }
        } else {
            if (success) {
                auiuAdapter.addItems(dataItems!!, true)
            }
            binding.recyclerView.loadFinished(success)
        }
    }

    fun enableLoadMore(callback: () -> Unit) {
        binding.recyclerView.enableLoadMore({
           if (refreshHeaderView.state == AuiuOverView.AuiuRefreshState.STATE_REFRESH) {
               binding.recyclerView.loadFinished(false)
               return@enableLoadMore
           }
            pageNum++
            callback()
        }, PRFETCH_SIZE)
    }

    fun disableLoadMore() {
        binding.recyclerView.disableLoadMore()
    }

    open fun createLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    }

    override fun enableRefresh(): Boolean {
        return true
    }

    @CallSuper
    override fun onRefresh() {
        if (binding.recyclerView.isLoading()) {
            binding.refreshLayout.post {
                binding.refreshLayout.refreshFinished()
            }
            return
        }

        pageNum = 1
    }

    companion object {
        const val PRFETCH_SIZE = 5
    }

}